package com.ak.ty.controller;


import com.ak.ty.service.impl.TyServiceImpl;
import com.ak.ty.utils.WjxUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.flyway.FlywayDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * @author bn
 */
@Slf4j
@Controller
public class TyController {

//    @RequestMapping("/wjx")
//    public String wjx() {
//        String url = "https://www.wjx.cn/jq/11891579.aspx";
//        // 直接调用问卷星
//        WjxUtils.wjx(url, "1$4}2$4}3$1}4$1}5$1}6$2}7$1}8$1}9$1}10$1}11$1");
//
//        return "访问成功";
//    }

    @Qualifier("MyScheduler")
    @Autowired
    Scheduler scheduler;

    @Autowired
    TyServiceImpl tyService;

    @RequestMapping("/addUser")
    @ResponseBody
    public String add(String id, String password, String email) {
        Boolean result = tyService.addUser(id, password, email);
        if (result) {
            return "添加成功";
        } else {
            return "添加失败";
        }
    }

    /**
     * 返回添加的页面
     *
     * @return
     */
    @RequestMapping("/add")
    public String add() {
        return "add";
    }

    @RequestMapping("/start")
    @ResponseBody
    public String start() {
        try {
            if (scheduler.isStarted()) {
                return "项目已经启动成功，请不要重复启动";
            }
            scheduler.start();
            return "项目启动成功";
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return "项目启动失败";
    }

    /**
     * 测试接口信息是否完整
     *
     * @return
     */
    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {

        return new Date().toString();
    }

    @RequestMapping("/testEmail")
    @ResponseBody
    public Boolean test(String to, String body) {
        return tyService.email(body, to);
    }

    /**
     * 返回测试浏览器的接口
     *
     * @return
     */
    @RequestMapping("/email")
    public String email() {
        return "email";
    }

}
